 //============= RECEIVE OSC COMMANDS =========================

// The subaddress define the variable or mode to change (see soundSpot for sound-sending variables).
// Some of these take VALUES.
// Example of a message: 
//     "/spot2/SendOSCMode/1" means start sending automatically in OSC mode
//     "/spot2/sendingPosition/1" means sending position (in any active mode). 
// char *subAddress[5]={ "/sendSerialMode", "/sendOSCMode", "/sendMIDIMode"};

void processOSC() {
  long values[8];
  
  // for tests:
  // logMessage(&recMes);
  
  // First, global commands (for all spots):
  if ( !strcmp( recMes.getAddress(0) , "resetAll" ) ) { 
         allSpotsInit();   
    } 
    else 
  {
   //Then, addressing of each spot (toplevel address matching means the spot identifier): 
   for (int i=0; i<NBSPOT; i++) {
          if( !strcmp( recMes.getAddress(0) ,  spotArray[i].spotName) ){ // in this case, we are refering to spot i 
                         //  Serial.print(topAddress[i]); 
               //Second level address matching for indicating which parameter to modify. We need to do this with a lot of "ifs":           
                // (a) First, the sending channel mode (serial, OSC or Midi), not exclusive: 
                if( !strcmp( recMes.getAddress(1) , "sendSerialMode" ) ) { 
                     // Check if it has to start or stop sending data on serial port: 
                     long data = recMes.getArgInt(0);  //get received data (should be only one in this case)
                     if (data==0) spotArray[i].sendSerialMode=false; else spotArray[i].sendSerialMode=true;
                }
                else if( !strcmp( recMes.getAddress(1) , "sendOSCMode" ) ) { 
                   // Serial.print(recMes.getAddress(1)); Serial.print(recMes.getArgInt(0)); 
                     // Check if it has to start or stop sending OSC data over Ethernet: 
                     long data = recMes.getArgInt(0);  //get received data (should be only one in this case)
                     if (data==0) spotArray[i].sendOSCMode=false; else spotArray[i].sendOSCMode=true;  
                    // for tests:
                    //  pinMode(30, OUTPUT); if (data==0) digitalWrite(30,LOW); else digitalWrite(30, HIGH);                 
                }
                 else if( !strcmp( recMes.getAddress(1) , "sendMIDIMode" ) ) { 
                     // Check if it has to start or stop sending Midi data: 
                    long data = recMes.getArgInt(0);  //get received data (should be only one in this case)
                    if (data==0) spotArray[i].sendMIDIMode=false; else spotArray[i].sendMIDIMode=true;                  
                }
                
                // (b) Then the sound sending parameters: 
                else if( !strcmp( recMes.getAddress(1) , "sendPosition" ) ) { 
                    long data = recMes.getArgInt(0);  //get received data (should be only one in this case)
                    if (data==0) spotArray[i].sendingPosition=false; else spotArray[i].sendingPosition=true;   
                    // pinMode(30, OUTPUT); if (data==0) digitalWrite(30,LOW); else digitalWrite(30, HIGH);                      
                }
                else if( !strcmp( recMes.getAddress(1) , "sendAngle" ) ) { 
                       long data = recMes.getArgInt(0);  //get received data (should be only one in this case)
                    if (data==0) spotArray[i].sendAngle=false; else spotArray[i].sendAngle=true;
                }
                // .....
                
                // (c) And finally, other parameters not related to sound: 
                else if( !strcmp( recMes.getAddress(1) , "kill" ) ) spotArray[i].alive = false;
                else if( !strcmp( recMes.getAddress(1) , "resucite" ) ) spotArray[i].alive = true;
                        
                // BEHAVIOUR: 
                // ****** NOTE: better in the future to call a setting function with more default parameters... 
                else if( !strcmp( recMes.getAddress(1) , "contourFollowing" ) ) spotArray[i].behaviourMode = COUNTOUR_FOLLOWING; 
                else if( !strcmp( recMes.getAddress(1) , "newtonDynamics" ) ) spotArray[i].behaviourMode =  NEWTON_DYNAMICS; 
                else if( !strcmp( recMes.getAddress(1) , "simpleBouncing" ) ) spotArray[i].behaviourMode = SIMPLE_BOUNCING; 
                else if( !strcmp( recMes.getAddress(1) , "snellLaw" ) ) spotArray[i].behaviourMode = SNELL_LAW; 
                
                // Other parameters:
                else if( !strcmp( recMes.getAddress(1) , "speedUp" ) ) { 
                  // long data = recMes.getArgInt(0);  
                   spotArray[i].vx*=2.0;spotArray[i].vy*=2.0;    
                }
                else if( !strcmp( recMes.getAddress(1) , "slowDown" ) ) { 
                   long data = recMes.getArgInt(0);  
                   spotArray[i].vx/=2.0;spotArray[i].vy/=2.0;    
                }
                else if( !strcmp( recMes.getAddress(1) , "radius" ) ) { 
                    // Get radius value: 
                    
                }
       
          }
      }
  }
}

// FOR TESTS ONLY (using the Serial port): 
void logMessage(OSCMessage *mes) {
  
    uint8_t *ip=mes->getIp();
  
    Serial.println(" ");
     //disp ip & port
    Serial.print("from IP:");
    Serial.print(ip[0],DEC);
    Serial.print(".");
    Serial.print(ip[1],DEC);
    Serial.print(".");
    Serial.print(ip[2],DEC);
    Serial.print(".");
    Serial.print(ip[3],DEC);
    Serial.print(" port:");
    Serial.print(mes->getPort(),DEC);
    Serial.print("   ");
    
    //disp adr
    for(int i = 0 ; i < mes->getAddressNum() ; i++){
      
      Serial.print(mes->getAddress(i));
       Serial.print(" ");
      
    }
    
    
    //disp type tags
    Serial.print("  ,");
    for(int i = 0 ; i < mes->getArgNum() ; i++){
      
      Serial.print(mes->getTypeTag(i));
      
    }
    Serial.print(" ");
   
   
   //disp args
    for(int i = 0 ; i < mes->getArgNum() ; i++){
      
      switch( mes->getTypeTag(i) ){
        
        case 'i': {
                      
                      Serial.print( mes->getArgInt(i) );
                  }
          break;
        
        case 'f':  {
                      
                      Serial.print( mes->getArgFloat(i) );
                  }
          break;
         
      }
      
       Serial.print(" ");
      
    }
    
    Serial.println(""); 
    
}

